#pragma once
#ifndef _KH_ENGINE_TILEDATA_H
#define _KH_ENGINE_TILEDATA_H

#include <Kharlia/Engine/Common.h>
#include <Kharlia/Engine/Units.h>

namespace kharlia { namespace engine {

// Primitive encoded tile type
typedef uint16 tile_t;

class KH_ENGINE_API FTileData {
    FSizeI mSize;
    std::vector<tile_t> mTiles;

    void CopyTilesImpl(FVector2I const& destpos, FRectI const& srcrect, FTileData* src);

public:
    FTileData(FSizeI const& size=FSizeI());
    FTileData(FSizeI const& size, str datastr);
    FTileData(FSizeI const& size, object sequence);
    virtual ~FTileData();

    inline const int GetTileCount() const { return mSize.GetArea(); }
    inline FSizeI const& GetSize() const { return mSize; }
    inline const std::vector<tile_t>& GetTiles() const { return mTiles; }
    inline tile_t GetTile(int index) const { return mTiles.at(index); }
    inline void SetTile(int index, tile_t value) { mTiles.at(index) = value; }

    void SetTiles(tile_t tile);
    void SetTiles(FVector2I const& topos, FSizeI const& size, tile_t tile);
    void SetTiles(FVector2I const& topos, FSizeI const& size, object sequence);
    void SetTiles(FVector2I const& topos, FSizeI const& size, str tiledatastr);
    void SetTiles(FVector2I const& topos, FSizeI const& size, tile_t const* tiles);
    void SetTilesFast(FVector2I const& topos, FSizeI const& size, object sequence);
    void CopyTiles(FVector2I const& destpos, shared_ptr<FTileData> src);
    void CopyTiles(FVector2I const& destpos, FRectI const& srcrect, shared_ptr<FTileData> src);

    list ToList() const;
    object ToString() const;

    str Repr() const;

    friend void _InitTileDataClasses();
    friend struct PSFTileData;
};

struct PSFTileData: public pickle_suite {
    static tuple getinitargs(FTileData& td);
};

}} //namespace Kharlia

#endif
